package com.fundstrategy.fund.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.fundstrategy.common.utils.DateUtils;
import com.fundstrategy.fund.domain.vo.FundSearchResultVo;
import com.sun.deploy.net.URLEncoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fundstrategy.fund.mapper.FundBasicMapper;
import com.fundstrategy.fund.domain.FundBasic;
import com.fundstrategy.fund.service.IFundBasicService;

/**
 * 基金基本信息Service业务层处理
 *
 * @author 邓先生
 * @date 2025-09-02
 */
@Service
@Slf4j
public class FundBasicServiceImpl implements IFundBasicService {


    private static final String SEARCH_API = "http://fundsuggest.eastmoney.com/FundSearch/api/FundSearchAPI.ashx?m=1&key=";
    private static final String DETAIL_API = "http://fund.eastmoney.com/pingzhongdata/";
    private static final String BASIC_INFO_API = "https://fundf10.eastmoney.com/FundArchivesDatas.aspx?type=jjcc&code=";


    @Autowired
    private FundBasicMapper fundBasicMapper;


    /**
     * 搜索基金（根据代码或名称）
     */

    @Override
    public List<FundSearchResultVo> searchFund(String keyword) {
        try {
            String url = SEARCH_API + URLEncoder.encode(keyword, "UTF-8");
            String jsonResponse = HttpUtil.get(url);

            JSONObject jsonObject = JSON.parseObject(jsonResponse);
            JSONArray datas = jsonObject.getJSONArray("Datas");

            List<FundSearchResultVo> results = new ArrayList<>();
            for (int i = 0; i < datas.size(); i++) {
                JSONObject item = datas.getJSONObject(i);
                FundSearchResultVo result = new FundSearchResultVo();
                result.setCode(item.getString("CODE"));
                result.setName(item.getString("NAME"));
                result.setType(item.getString("FTYPE"));
                results.add(result);
            }
            return results;
        } catch (Exception e) {
            log.error("搜索基金失败: {}", keyword, e);
            return Collections.emptyList();
        }
    }

    /**
     * 查询基金基本信息
     *
     * @param id 基金基本信息主键
     * @return 基金基本信息
     */
    @Override
    public FundBasic selectFundBasicById(Long id) {
        return fundBasicMapper.selectFundBasicById(id);
    }

    /**
     * 查询基金基本信息列表
     *
     * @param fundBasic 基金基本信息
     * @return 基金基本信息
     */
    @Override
    public List<FundBasic> selectFundBasicList(FundBasic fundBasic) {
        return fundBasicMapper.selectFundBasicList(fundBasic);
    }

    /**
     * 新增基金基本信息
     *
     * @param fundBasic 基金基本信息
     * @return 结果
     */
    @Override
    public int insertFundBasic(FundBasic fundBasic) {
        fundBasic.setCreateTime(DateUtils.getNowDate());
        return fundBasicMapper.insertFundBasic(fundBasic);
    }

    /**
     * 修改基金基本信息
     *
     * @param fundBasic 基金基本信息
     * @return 结果
     */
    @Override
    public int updateFundBasic(FundBasic fundBasic) {
        fundBasic.setUpdateTime(DateUtils.getNowDate());
        return fundBasicMapper.updateFundBasic(fundBasic);
    }

    /**
     * 批量删除基金基本信息
     *
     * @param ids 需要删除的基金基本信息主键
     * @return 结果
     */
    @Override
    public int deleteFundBasicByIds(Long[] ids) {
        return fundBasicMapper.deleteFundBasicByIds(ids);
    }

    /**
     * 删除基金基本信息信息
     *
     * @param id 基金基本信息主键
     * @return 结果
     */
    @Override
    public int deleteFundBasicById(Long id) {
        return fundBasicMapper.deleteFundBasicById(id);
    }
}
